﻿using MetroFramework.Forms;
using System;
using System.IO;
using System.IO.Ports;
using System.Windows.Forms;

namespace PassportReaderAPI
{
    public partial class FormSetting : MetroForm
    {
        private PassportReaderOption _option = new PassportReaderOption();
        public PassportReaderOption Option
        {
            get { return _option; }
            set { _option = value; }
        }

        public FormSetting()
        {            
            InitializeComponent();
            LoadConfig();
        }

        private void LoadConfig()
        {
            if (!File.Exists(_option.ConfigFilePath))
            {
                _option.Save();
            }
            _option.Load();
        }


        private void InitCtrls()
        {
            try
            {
                numericUpDownPassportTId.Value = _option.PassportTypeId;
                numericUpDownVisaTId.Value = _option.VisaTypeId;
                textBoxDllDir.Text = _option.DllDir;
                textBoxUserId.Text = _option.UserId;
                metroComboBoxRetryTime.SelectedIndex = _option.RetryTimes - 1;
                intRangeTrackBarScanInterval.Value = _option.ReadInterval;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }         
        }

        private bool UpdateData()
        {
            if(metroComboBoxRetryTime.SelectedIndex == -1)
            {
                MessageBox.Show("没有选择重试次数");
                return false;
            }
            if(!Directory.Exists(textBoxDllDir.Text))
            {
                MessageBox.Show("选择的目录无效");
                return false;
            }
            _option.UserId = textBoxUserId.Text;
            _option.VisaTypeId = (int)numericUpDownPassportTId.Value;
            _option.PassportTypeId = (int)numericUpDownVisaTId.Value;
            _option.ReadInterval = intRangeTrackBarScanInterval.Value;
            _option.RetryTimes = metroComboBoxRetryTime.SelectedIndex + 1;
            _option.DllDir = textBoxDllDir.Text;
            return _option.Save();
        }

    
        private void buttonCancell_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.Close();
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            if (UpdateData())
            {
                this.DialogResult = System.Windows.Forms.DialogResult.OK;
                this.Close();
            }           
        }

        private void FormIDCReaderSetting_Load(object sender, EventArgs e)
        {
            InitCtrls();
        }

        private void buttonSelect_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dialog = new FolderBrowserDialog();
            dialog.SelectedPath = textBoxDllDir.Text;
            dialog.Description = "请选择识别核心类库的目录";
            if(dialog.ShowDialog() == DialogResult.OK)
            {
                textBoxDllDir.Text = dialog.SelectedPath;
            }
        }
    }
}
